package admin

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go_my_framework/app/dao"
	"go_my_framework/app/global/request"
	"go_my_framework/app/service"
	"go_my_framework/util/base"
	"strconv"
)

func RoleIndex(c *gin.Context) {
	if c.Request.Method == "POST" {
		var f request.RoleForm
		if err := c.ShouldBind(&f); err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		svc := service.RoleService{}
		list, count, err := svc.GetRoleList(f)
		if err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		data := make(map[string]interface{})
		data["data"] = list
		data["count"] = count
		base.Error(c, "", data)
		return
	} else {
		name := c.Query("name")
		base.Html(c, "/admin/role/index", gin.H{"name": name})
	}
}

func RoleAdd(c *gin.Context) {
	if c.Request.Method == "POST" {
		name := c.PostForm("name")
		desc := c.PostForm("desc")
		superStr := c.PostForm("super")
		super, _ := strconv.ParseInt(superStr, 10, 8)
		svc := service.RoleService{}
		err := svc.AddRole(name, desc, int8(super))
		if err != nil {
			base.Error(c, err.Error(), nil)
			return
		} else {
			base.Success(c, "提交成功", nil)
			return
		}
	} else {
		base.Html(c, "role/add", nil)
	}
}

func RoleEdit(c *gin.Context) {
	if c.Request.Method == "POST" {
		idStr := c.PostForm("id")
		name := c.PostForm("name")
		desc := c.PostForm("desc")
		superStr := c.PostForm("super")
		id, _ := strconv.Atoi(idStr)
		super, _ := strconv.ParseInt(superStr, 10, 8)

		svc := service.RoleService{}
		err := svc.EditRole(id, name, desc, int8(super))
		if err != nil {
			base.Error(c, err.Error(), nil)
			return
		} else {
			base.Success(c, "提交成功", nil)
			return
		}
	} else {
		idStr := c.Query("id")
		id, _ := strconv.Atoi(idStr)
		svc := service.RoleService{}
		data, _ := svc.FindRole(id)
		base.Html(c, "/admin/role/edit", gin.H{
			"name": data.Name,
			"desc": data.Desc,
			"super": data.Super,
			"id": data.Id,
		})
	}
}

func RoleDel(c *gin.Context) {
	idStr := c.PostForm("id")
	id, _ := strconv.Atoi(idStr)
	svc := service.RoleService{}
	err := svc.DelRole(id)
	if err != nil {
		base.Error(c, err.Error(), nil)
		return
	} else {
		base.Success(c, "已删除", nil)
		return
	}
}

func Privilege(c *gin.Context) {
	if c.Request.Method == "POST" {
		var f struct{
			Id         int   `json:"id" form:"id"`
			PurviewIds []int `json:"purview_ids[]" form:"purview_ids[]" binding:"required,gt=0"`
		}
		if err := c.ShouldBind(&f); err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		if len(f.PurviewIds) == 0 {
			base.Error(c, "请选择权限", nil)
			return
		}
		svc := service.AdminService{}
		err := svc.Privilege(f.Id, f.PurviewIds)
		if err != nil {
			base.Error(c, err.Error(), nil)
			return
		} else {
			base.Success(c, "已提交", nil)
			return
		}
	} else {
		idStr := c.Query("id")
		id, _ := strconv.Atoi(idStr)
		existing, _ := dao.NewRolePurviewDaoImpl().GetPurviewListByRoleId(id)
		var existPurviewId = make([]int, len(existing), len(existing))
		for _, v := range existing {
			existPurviewId = append(existPurviewId, v.AdminPurviewId)
		}

		svc := service.PurviewService{}
		list := svc.GetAll(existPurviewId)
		jsonList, _ := json.Marshal(list)
		base.Html(c, "/admin/role/privilege", gin.H{"id": id, "list": string(jsonList)})
	}
}